package com.tw.modules.web;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tw.modules.common.ConvertJson;
import com.tw.modules.entity.User;
import com.tw.modules.service.UserService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maxiaodong
 * @since 2018-04-05
 */
@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userserviceimpl;
	/**
	 * 主页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/", method = { RequestMethod.GET })
	public String index() {

		return "index";

	}

	/**
	 * 登录(测试通过)
	 * 
	 * @param uUid
	 * @param uPassword
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/judgeLogin", method = { RequestMethod.GET })
	public String findById(@RequestParam("uPhone") long uPhone, @RequestParam("uPassword") String password) {
		if (userserviceimpl.selectById(uPhone).getuPassword().equals(password)) {
			return ConvertJson.responseJSON("success");
		}
		return ConvertJson.responseJSON("fail");
	}
	
	/**
	 * 注册(测试通过)
	 * @param uphone 手机号
	 * @param password 密码
	 * @param username 昵称
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/register", method = { RequestMethod.POST })
	public String register(@RequestParam("u_phone")long uphone,@RequestParam("password")String password,@RequestParam("username")String username ){
		if (userserviceimpl.selectById(uphone)!=null) {
			return ConvertJson.responseJSON("fail","手机号已存在");
		}else{
			User user = new User();
			user.setuUsername(username);//昵称
			user.setuPhone(uphone);//手机号
			user.setuPassword(password);//密码
			boolean flag = userserviceimpl.insert(user);
			System.out.println(flag);
			if (flag) {
				return ConvertJson.responseJSON("success","注册成功");
			}
			return ConvertJson.responseJSON("fail");
		}
		
	}
	
	/**
	 * 修改用户(测试通过)
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateUser", method = { RequestMethod.GET })
	public String updateUser(@RequestAttribute("user")User user){
		boolean flag = userserviceimpl.updateById(user);
		if (flag) {
			ConvertJson.responseJSON("success","修改成功");
		}
		return ConvertJson.responseJSON("fail","修改失败");
	}
	
	/**
	 * 删除用户(测试通过)
	 * @param uid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteUser", method = { RequestMethod.GET })
	public String deleteUser(@RequestParam("phone")long phone){
		boolean flag = userserviceimpl.deleteById(phone);
		if (flag) {
			return ConvertJson.responseJSON("success");
		}
		return ConvertJson.responseJSON("fail");
	}
}
